package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.myapplication.database.room.pojo.Users;
import com.example.myapplication.enity.User;

import java.util.ArrayList;

public class ProviderActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_procider);
        findViewById(R.id.query1).setOnClickListener(this);
        findViewById(R.id.add1).setOnClickListener(this);
        findViewById(R.id.update).setOnClickListener(this);
        findViewById(R.id.delete1).setOnClickListener(this);
        ContentResolver contentResolver = getContentResolver();
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyDBProvider");
        contentResolver.registerContentObserver(uri, true, new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Log.i("ProviderActivity","数据发生变化！");
            }
        });
    }

    public void query(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyDBProvider");
        ContentResolver resolver = getContentResolver();
        //Cursor cursor = resolver.query(uri, null, null, null, null);
        // 条件查询
        Cursor cursor = resolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<User> list = new ArrayList<>();
            while (cursor.moveToNext()) {
                User user = new User();
                user.id = cursor.getInt(0);
                user.name = cursor.getString(1);
                user.age = cursor.getInt(2);
                user.height = cursor.getLong(3);
                user.weight = cursor.getFloat(4);
                //SQLite没有布尔型，用0表示false，用1表示true
                user.married = cursor.getInt(5) != 0;
                list.add(user);
            }
            Toast.makeText(this, "查询结果为"+list.size()+"条："+list.toString(), Toast.LENGTH_SHORT).show();
        }
    }

    public void queryRoom(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyRoomProvider");
        ContentResolver resolver = getContentResolver();
        //Cursor cursor = resolver.query(uri, null, null, null, null);
        // 条件查询
        Cursor cursor = resolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<Users> list = new ArrayList<>();
            while (cursor.moveToNext()) {
                Users user = new Users();
                user.setUid(cursor.getInt(0));
                user.setUserName(cursor.getString(1));
                user.setUserAge(cursor.getInt(2));
                user.setUserHeight(cursor.getInt(3));
                list.add(user);
            }
            Toast.makeText(this, "查询结果为"+list.size()+"条："+list.toString(), Toast.LENGTH_SHORT).show();
        }
    }

    public void insert(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyDBProvider");
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "SmartDragon");
        values.put("age", 22);
        values.put("height", 180);
        values.put("weight", 76);
        values.put("married", 0);
        Uri insert = resolver.insert(uri, values);
        if (insert !=null){
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        }
    }

    public void insertRoom(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyRoomProvider");
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "SmartDragon");
        values.put("age", 22);
        values.put("height", 180);
        values.put("weight", 76);
        values.put("married", 0);
        Uri insert = resolver.insert(uri, values);
        if (insert !=null){
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        }
    }

    public void update(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyDBProvider");
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("age","999");
        int update = resolver.update(uri,values,"name =?",new String[]{"SmartDragon"});
        if(update == 1){
            Toast.makeText(this,"更新成功！", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"更新失败！", Toast.LENGTH_SHORT).show();
        }
    }

    public void updateRoom(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyRoomProvider");
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name","SmartDragon");
        values.put("age", 999);
        int update = resolver.update(uri,values,"name =?",new String[]{"SmartDragon"});
        if(update == 1){
            Toast.makeText(this,"更新成功！", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"更新失败！", Toast.LENGTH_SHORT).show();
        }
    }

    public void delete(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyDBProvider");
        ContentResolver resolver = getContentResolver();
        int delete = resolver.delete(uri, "name = ?", new String[]{"SmartDragon"});
        if(delete == 1){
            Toast.makeText(this,"删除成功！", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"删除失败！", Toast.LENGTH_SHORT).show();
        }
    }

    public void deleteRoom(View view) {
        Uri uri = Uri.parse("content://com.example.myapplication.provider.MyRoomProvider");
        ContentResolver resolver = getContentResolver();
        int delete = resolver.delete(uri, "name = ?", new String[]{"SmartDragon"});
        if(delete == 1){
            Toast.makeText(this,"删除成功！", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"删除失败！", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add1:
                //insert(findViewById(R.id.add1));
                insertRoom(findViewById(R.id.add1));
                break;
            case R.id.query1:
                //query(findViewById(R.id.query1));
                queryRoom(findViewById(R.id.query1));
                break;
            case R.id.delete1:
                //delete(findViewById(R.id.delete1));
                deleteRoom(findViewById(R.id.delete1));
                break;
            case R.id.update:
                //update(findViewById(R.id.update));
                updateRoom(findViewById(R.id.update));
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}